﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyIni;
using System.Windows.Forms;

namespace CadStrategy2013.CgqStyle
{
    public class BzdZlClass:Cgq2013
    {
        string strDingXing1;
        string strDingXing2;
        string strFenXianPan;
        string strZuHe;
        string bzdName;
        public ZLClass zlClass=new ZLClass("站联");
        public BzdZlClass(string secname)
            : base(secname)
        {
            //zlClass = new ZLClass("站联");
        }
        public BzdZlClass(string secname, Dictionary<string, string> dic1, Dictionary<string, string> dic2)
            : base(secname, dic1)
        {
            zlClass.portDic = dic2;
        }
        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("半自动");
        //    zlClass.GetCfg();
        //}
        public override List<string> ReadTemplateDxf()
        {
            return ReadDxf(Path + "\\template\\半自动站联模板.dll");
        }
        public override string GetSaveFileName(int index)
        {
            if(ShiJiTuZhiCount==1)
                return "\\半自动站联.dxf";
            return "\\半自动站联"+(index+1).ToString()+".dxf";
            //if (index == 0)
            //    return "\\半自动站联.dxf";
            //else
            //    return "\\半自动站联" + index + ".dxf";
        }
        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();
            
            IniFile inifile = new IniFile("./\\" + StationName + "\\bzdzl.txt");
            int sum0 = inifile.GetInt16("半自动", "sum", 0);
            string str1s = inifile.GetString("半自动", "定型", "");
            GetBzdDingXing(str1s);
            for (int i = 0; i < sum0; i++)
            {
                DevClass devClass = new DevClass();
                string strs = inifile.GetString("半自动", (i + 1).ToString(), "");
                if (GetBzdF_Zh(strs))
                {
                    devClass.DevName = bzdName;
                    devClass.peiList.Add(strFenXianPan + "-1");
                    devClass.peiList.Add(strFenXianPan + "-2");
                    devClass.cpeiList.Add(strDingXing1);
                    devClass.cpeiList.Add(strZuHe + strDingXing2);
                    dxfDataClass.devList.Add(devClass);
                }
                else
                {
                    devClass.DevName = bzdName;
                    devClass.peiList.Add("");
                    devClass.peiList.Add("");
                    devClass.cpeiList.Add("");
                    devClass.cpeiList.Add("");
                    dxfDataClass.devList.Add(devClass);
                }
            }
            if (sum0 != 0)
            {
                ShiJiBanCount = dxfDataClass.devList.Count / BanDevCount + 1;
                ShiJiTuZhiCount = ShiJiBanCount / BanCount + 1;
            }
            else
            {
                ShiJiBanCount = 0;
                ShiJiTuZhiCount = 0;
            }
            ShiJiTuZhiCount = ShiJiTuZhiCount < zlClass.ShiJiTuZhiCount ? zlClass.ShiJiTuZhiCount : ShiJiTuZhiCount;
            m_dxfDataClass = dxfDataClass;
           // m_dxfDataClass = zlClass.GetZlDxfData(dxfDataClass);
            return dxfDataClass;
           
        }
        public override void WriteDxf()
        {
            List<string> list;
            List<string> list1 = ReadTemplateDxf();
          
            //base.WriteDxf(list, SavePath +":\\" + StationName + GetSaveFileName(FileNameIndex));
            for (int i = 0; i <= FileNameIndex; i++)
            {
                list = DealData(list1, i);
                list = zlClass.DealData(list, i);
                base.WriteDxf(list, SavePath + GetSaveFileName(i));
            }
            MessageBox.Show("ok");
        }
        //public  override List<string> DealData(List<string> strList)
        //{  
        //    List<string> list = new List<string>();
        //    foreach (string str in strList)
        //    {
        //        string str1 = str;
        //        str1=ReplacebNo(str1);
        //        str1=ReplaceCPei(str1);
        //        str1=ReplaceDev(str1);
        //        str1=ReplacePei(str1);
        //        list.Add(str1);
        //    }
            
        //    return list;
        //}
        private string[] GetBzdDingXing(string str1s)
        {
            string[] strDingXings = str1s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (strDingXings.Length > 1)
            {
                strDingXing1 = strDingXings[0];
                strDingXing2 = strDingXings[1];
            }
            else
            {
                strDingXing1 ="";
                strDingXing2 ="";
            }
            return new string[] { strDingXing1, strDingXing2 };
        }
        private bool GetBzdF_Zh(string str)
        {
            string[] strDevs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (strDevs.Length > 2)
            {
                bzdName = strDevs[0];
                strFenXianPan = strDevs[1];
                strZuHe = strDevs[2];
                return true;
            }
            else
            {
                if (strDevs.Length > 0)
                    bzdName = strDevs[0];
                else
                {
                    bzdName = "";
                    strFenXianPan = "";
                    strZuHe = "";
                }
                return false;
            }
        }
        //private string[] GetBianHao()
        //{
        //    if (ShiJiBanHao == null)
        //    {
        //        string[] str = new string[ShiJiBanCount];
        //        for (int i = 0; i < ShiJiBanCount; i++)
        //        {
        //            Strategy2013.SetBianHao();
        //            str[i] = Strategy2013.bNo;
        //        }
        //        ShiJiBanHao = str;
        //    }
        //    return ShiJiBanHao;
        //}
        //private string ReplacebNo(string str)
        //{
        //    string[] strs = GetBianHao();
        //    for (int i = 1; i <= ShiJiBanCount; i++)
        //    {
        //        if (str.Contains("bNo" + i))
        //        {
        //            str=str.Replace("bNo" + i, strs[i - 1]);
        //        }
        //    }
        //    return str;
        //}
        //private string ReplaceDev(string str2)
        //{
        //    string str = str2;
        //    for (int j = 1; j <= ShiJiBanCount; j++)
        //    {
        //        for (int i = 1; i <= BanDevCount; i++)
        //        {
        //            string str1="dev" + j.ToString() + "-" + i.ToString();
        //            if (str.Contains(str1))
        //            {
        //                int index=(j-1)*BanDevCount+i-1;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    break;
        //                try
        //                {
        //                   str= str.Replace(str1, m_dxfDataClass.devList[index].DevName);
        //                }
        //                catch (Exception e)
        //                {

        //                }
        //            }
        //        }
        //    }
        //    return str;
        //}
        //private string ReplacePei(string str)
        //{
        //    for (int j = 1; j <= ShiJiBanCount; j++)
        //    {
        //        for (int i = 1; i <= 2*BanDevCount; i++)
        //        {
        //            string str1 = "pei" + j.ToString() + "-" + i.ToString();
        //            if (str.Contains(str1))
        //            {
        //                int index = (j - 1) * BanDevCount + (i - 1)/2;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    break;
        //                int index1=(i-1)/2;
        //                str=str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
        //            }
        //        }
        //    }
        //    return str;
        //}
        //private string ReplaceCPei(string str)
        //{
        //    for (int j = 1; j <= ShiJiBanCount; j++)
        //    {
        //        for (int i = 1; i <= 2 * BanDevCount; i++)
        //        {
        //            string str1 = "cpei" + j.ToString() + "-" + i.ToString();
        //            if (str.Contains(str1))
        //            {
        //                int index = (j - 1) * BanDevCount + (i - 1) / 2;
        //                if (index >= m_dxfDataClass.devList.Count)
        //                    break;
        //                int index1 = (i - 1) %2;
        //                str=str.Replace(str1, m_dxfDataClass.devList[index].cpeiList[index1]);
        //            }
        //        }
        //    }
        //    return str;
        //}
        public override int GetFileNameIndex()
        {
            int i= base.GetFileNameIndex();
            int j = zlClass.GetFileNameIndex();
            return FileNameIndex= Math.Max(i, j);
        }

        public override string ReplacebNo(string str, int fi = 0)
        {
            string[] strs = GetBianHao();
            for (int i = 1; i <= BanCount; i++)
            {
                int n = i;// +fi * BanCount;
                if (str.Contains("bNo" + n + ">"))
                {
                    if ((i + fi * BanCount) <= ShiJiBanCount)
                    {
                        return str = str.Replace("bNo" + n + ">", strs[(fi * BanCount) + i - 1]);
                    }
                    else
                    {
                        return "";// str = str.Replace(str, "");
                    }
                }
            }
            return str;
        }
        public override string ReplaceDev(string str2, int fi = 0)
        {
            string str = str2;
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;// -fi * BanCount;
                for (int i = 1; i <= BanDevCount; i++)
                {
                    string str1 = "dev" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + j - 1) * BanDevCount + i - 1;
                        if (index >= m_dxfDataClass.devList.Count)
                        {
                            return str = "";
                        }
                        else
                        {
                            try
                            {
                                return str = str.Replace(str1, m_dxfDataClass.devList[index].DevName);
                            }
                            catch
                            {
                                return str = "";
                            }
                        }
                    }
                }
            }
            return str;
        }
        public override string ReplacePei(string str, int fi = 0)
        {
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;// -fi * BanCount;
                for (int i = 1; i <= BanDevPeiCount * BanDevCount; i++)
                {
                    string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + j - 1) * BanDevCount + (i - 1) / BanDevPeiCount;
                        if (index >= m_dxfDataClass.devList.Count)
                        {
                            return "";
                        }
                        else
                        {
                            int index1 = (i - 1) % (BanDevPeiCount);//BanDevCount);
                            try
                            {
                                return str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
                            }
                            catch
                            {
                                return "";
                            }
                        }
                    }
                }
            }
            return str;
        }
        public override string ReplaceCPei(string str, int fi = 0)
        {
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;
                for (int i = 1; i <= BanDevJkbCount * BanDevCount; i++)
                {
                    string str1 = "cpei" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + j - 1) * BanDevCount + (i - 1) / BanDevJkbCount;
                        if (index >= m_dxfDataClass.devList.Count)
                        {
                            return "";
                        }
                        else
                        {
                            int index1 = (i - 1) % BanDevJkbCount;
                            try
                            {
                                return str = str.Replace(str1, m_dxfDataClass.devList[index].cpeiList[index1]);
                            }
                            catch
                            {
                                return "";
                            }
                        }
                    }
                }
            }
            return str;
        }
    }
}
